﻿using BasicInformation.ApiWrite.Application.Command.Client;
using BasicInformation.Domain;
using BasicInformation.ErrorCode;
using BasicInformation.Infrastructure.Implement;
using MediatR;

namespace BasicInformation.ApiWrite.Application.Handler.Client
{
    /// <summary>
    /// 客户管理删除处理类
    /// </summary>
    public class DelClientHandler : IRequestHandler<DelClientCommand, ApiResult<int>>
    {
        private readonly DatabaseContext db;
        private readonly ILogger<DelClientHandler> logger;

        public DelClientHandler(DatabaseContext db,ILogger<DelClientHandler> logger)
        {
            this.db = db;
            this.logger = logger;
        }
        public async Task<ApiResult<int>> Handle(DelClientCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var client = db.SqlSugar.Queryable<ClientModel>().Where(x => x.ClientId == request.ClientId).First();
            client.IsDel= true;
            int r = await db.SqlSugar.Updateable(client).ExecuteCommandAsync();
            if (r > 0)
            {
                result.Msg = "客户管理删除成功";
                result.Data = r;
                result.Code = ApiEnum.OK;
                logger.LogInformation(result.Msg);
            }
            else
            {
                result.Msg = "客户管理删除失败";
                result.Data = r;
                result.Code = ApiEnum.Fail;
                logger.LogError(result.Msg);
            }
            return await Task.FromResult(result);
        }
    }
}
